/* cl.ext.InputSearch 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery'], factory);
        } else if (typeof module === 'object' && module.exports) {
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery);
            };
        } else {
            factory(jQuery);
        }
    }(
    function ($){
        var defaults={
            txtId:"",
            iconId:"",
            placeholder:"",
            initValue:"",
            searchCallback:null
        };
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (elementData,options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                var optionBase =$.ExtCommon.initMergeJson(elementData,options);
                optionBase =$.ExtCommon.initMergeJson(this.getDefaults(),optionBase);
                return optionBase
            },
            getHtml:function (options) {
                var inputId=$.ExtCommon.isCheckEmpty(options.txtId)?"":$.ExtCommon.getStringConnect('id="{0}"',[options.txtId]);
                var input=$.ExtCommon.getStringConnect('<input type="text" class="form-control" {0} placeholder="{1}">',[inputId,options.placeholder]);
                var iconId=$.ExtCommon.isCheckEmpty(options.iconId)?"":$.ExtCommon.getStringConnect('id="{0}"',[options.iconId]);
                var icon=$.ExtCommon.getStringConnect('<i class="fa fa-search form-control-icon" {0}></i>',[iconId]);
                return input+icon;
            }
        };
        var modalOperateElement=function(element,options){
            this.elementModal = {};
            this.optionModal=options;
            this.elementModal.parent=element;
            this.elementModal.txt=element.find(":text");
            this.elementModal.icon=element.find(".form-control-icon");
        };
        modalOperateElement.prototype = {
            init:function(){
                var self=this;
                self.bindValue();
                self.bindEvent();
            },
            bindValue:function () {
                if(!$.ExtCommon.isCheckEmpty(this.optionModal.initValue)){
                    this.setValue(this.optionModal.initValue);
                }
            },
            bindEvent:function(){
                var self=this;
                self.elementModal.txt.off("keydown").on("keydown", function(e){
                    if(e.keyCode === 13) {self._searchCallback()}
                });
                self.elementModal.icon.off("click").on("click", function(){
                    self._searchCallback()
                });
            },
            _searchCallback:function () {
                if(this.optionModal.searchCallback){
                    var value=$.trim(this.elementModal.txt.val());
                    this.optionModal.searchCallback(value);
                }
            },
            getValue:function () {
                return  $.trim(this.elementModal.txt.val());
            },
            setValue:function (value) {
                this.elementModal.txt.val(value)
            }
        };
        var InputSearchExtModal = function(element, options) {
            var self=this;
            self.element = $(element);
            self.optionBase=$modalCommon.getOptions(self.element.data(),options);
            self.element.addClass("panel-input-search-parent").html($modalCommon.getHtml(self.optionBase));
            this.elementOperate=new modalOperateElement(this.element,this.optionBase);
            this.elementOperate.init();
        };
        InputSearchExtModal.prototype = {
            getValue:function () {
                return this.elementOperate.getValue();
            },
            setValue:function (value) {
                return this.elementOperate.setValue(value);
            }
        };
        $.fn.ExtInputSearch = function(options) {
            var dataFun;
            this.each(function() {
                var el = $(this);
                var tempData=el.data('ExtInputSearch');
                if (tempData) {
                    if (typeof  options === "string") {
                        if (tempData[options]) {
                            dataFun =tempData[options]()
                        } else {
                            dataFun = tempData
                        }
                    } else {
                        dataFun = tempData
                    }
                } else {
                    dataFun = new InputSearchExtModal(el, options);
                    el.data('ExtInputSearch', dataFun);
                }
            });
            return dataFun;
        };
        return InputSearchExtModal;
    })
);
